package com.gqr.threadcoreknowledge.stopthreads;

/**
 * while内try/catch的问题
 * Copyright (C), 2020-2024,gqr
 * FileName:CantInterrupt
 * author:guqingrong
 * Date:2024-04-16 22:27
 * Description: 如果while里面放try/catch,会导致中断失效
 * 原因是catch的异常 又重新进入循环 没有满组循环跳出的条件
 * sleep函数 一旦响应中断 于是便会把 interrupted标记位清除 即Thread.currentThread().isInterrupted()不是true
 * 在循环内相应到了中断 也catch住了
 * History:
 * <author>    <time>    <version>    <desc>
 * 作者姓名     修改时间     版本号        描述
 */
public class CantInterrupt {
    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = ()->{
            int num = 0;
            while(num<=10000){
                if(num% 100==0){
                    System.out.println(num+"是100的倍数");
                }
                num++;
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
                    e.printStackTrace();
                }
            };
        };
        Thread thread = new Thread(runnable);
        thread.start();
        Thread.sleep(5000);
        thread.interrupt();
    }
}
